﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace IGolovkine.ShoutCastRipper.Service.Configuration
{
    public abstract class ConfigurationElementCollection<T> : ConfigurationElementCollection, 
        IEnumerable<T> where T : ConfigurationElement, new()
    {
        public ConfigurationElementCollection(string addElementName)
        {
            this.AddElementName = addElementName;
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new T();
        }

        public T this[int index]
        {
            get { return (T)this.BaseGet(index); }
        }

        public new IEnumerator<T> GetEnumerator()
        {
            for (int i = 0; i < this.Count; i++)
            {
                yield return this[i];
            }
        }

        protected sealed override object GetElementKey(ConfigurationElement element)
        {
            return this.GetElementKey(((T)element));
        }

        protected abstract object GetElementKey(T element);

        public IEnumerable<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter)
        {
            foreach (T el in this)
            {
                yield return converter(el);
            }
        }
    }
}
